/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.database.dao;

import com.database.DatabaseTableProjectionGenerator;
import com.database.sql_generator.BeberageSQLGenerator;
import com.database.tables.BeberagesTable;
import com.domain.Beberage;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Miguel
 */
public class BeberageAccessObject extends DataAccessObject<Beberage> {

    public BeberageAccessObject(String dbTable) {
        super(dbTable);
        setSQLGenerator(new BeberageSQLGenerator(dbTable));
    }

    @Override
    public int insertObject(Beberage object) {
        String selectStatement = getSQLGenerator().createInsertStatement(object);
        Statement sqlStatement = getStatement();
        int result = DataAccessObject.ERROR_EXECUTING_OPERATION;
        try {
            result = sqlStatement.executeUpdate(selectStatement);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            getConnectionManager().closeConnection();
        }
        return result;
    }

    @Override
    public int updateObject(Beberage prevObject, Beberage newObject) {
        String updateStatement = getSQLGenerator().createUpdateStatement(prevObject, newObject);
        Statement sqlStatement = getStatement();
        int result = DataAccessObject.ERROR_EXECUTING_OPERATION;
        try {
            result = sqlStatement.executeUpdate(updateStatement);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            getConnectionManager().closeConnection();
        }
        return result;
    }

    @Override
    public ArrayList<Beberage> selectDataFromDatabase(DatabaseTableProjectionGenerator tableProjection, String condition) {
        Statement sqlStatement = getStatement();
        ArrayList<Beberage> beberageList = null;
        try {
            String selectStatement = getSQLGenerator().createSelectStatement(tableProjection, condition);
            ResultSet selectResult = sqlStatement.executeQuery(selectStatement);
            beberageList = getBeberagesFromResult(selectResult);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            getConnectionManager().closeConnection();
        }
        return beberageList;
    }

    @Override
    public int deleteObject(Beberage object) {
        Statement sqlStatement = getStatement();
        int result = DataAccessObject.ERROR_EXECUTING_OPERATION;
        try{
            String deleteQuery = getSQLGenerator().createDeleteStatement(object);
            result = sqlStatement.executeUpdate(deleteQuery);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            getConnectionManager().closeConnection();
        }
        return result;
    }

    private Statement getStatement() {
        DatabaseConnectionManager connManager = getConnectionManager();
        connManager.openConnection();
        Connection dbConnection = connManager.getConnection();
        Statement sqlStatement = null;
        try {
            sqlStatement = dbConnection.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(BeberageAccessObject.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sqlStatement;
    }

    private ArrayList<Beberage> getBeberagesFromResult(ResultSet inResult) {
        ArrayList<Beberage> beberagesList = new ArrayList<Beberage>();
        try {
            while (inResult.next()) {
                Beberage actualBeberage = new Beberage();
                int beberageID = inResult.getInt(BeberagesTable.ID_COL);
                actualBeberage.setID(beberageID);
                
                String beberageName = inResult.getString(BeberagesTable.BEBERAGE_NAME_COL);
                actualBeberage.setName(beberageName);
                
                String beberageDescr = inResult.getString(BeberagesTable.BEBERAGE_DESCR_COL);
                actualBeberage.setDescription(beberageDescr);
                
                String beberageImagePath = inResult.getString(BeberagesTable.BEBERAGE_IMAGE_PATH_COL);
                actualBeberage.setImagePath(beberageImagePath);
                
                beberagesList.add(actualBeberage);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return beberagesList;
    }
}
